package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.InspectionEnums;
import com.mxpio.erp.common.quality.entity.InspectionBill;
import com.mxpio.erp.common.quality.entity.InspectionBillCheckDetail;
import com.mxpio.erp.common.quality.service.InspectionBillCheckDetailService;
import com.mxpio.erp.common.quality.service.InspectionBillService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InspectionBillCheckDetailController", description = "质检单据-样本检测项-明细接口")
@RestController
@RequestMapping("/erp/quality/ibcheckdetail/")
public class InspectionBillCheckDetailController {

    @Autowired
    private InspectionBillCheckDetailService service;
    @Autowired
    private InspectionBillService inspectionBillService;
    @GetMapping("list")
    @Operation(summary = "质检单据-样本检测项-明细列表", description = "获取质检单据-样本检测项-明细列表（无分页）", method = "GET")
    public Result<List<InspectionBillCheckDetail>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InspectionBillCheckDetail> inspectionBillSimple = service.list(InspectionBillCheckDetail.class, criteria);
        return Result.OK(inspectionBillSimple);
    }

    @GetMapping("page")
    @Operation(summary = "质检单据-样本检测项-明细列表", description = "获取质检单据-样本检测项-明细列表（分页）", method = "GET")
    public Result<Page<InspectionBillCheckDetail>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InspectionBillCheckDetail> inQualityOrderLines = service.listPage(InspectionBillCheckDetail.class, page, criteria);
        return Result.OK(inQualityOrderLines);
    }
    
    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取质检单据-样本检测项-明细", description = "根据bizNo获取质检单据-样本检测项-明细", method = "GET")
    public Result<List<InspectionBillCheckDetail>> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<InspectionBillCheckDetail> inQualityOrderLines = service.list(InspectionBillCheckDetail.class, c);
        return Result.OK(inQualityOrderLines);
    }

    @GetMapping("listbysimple/{bizNo}/{simpleCode}")
    @Operation(summary = "根据bizNo,simpleCode获取对应质检单据-样本检测项-明细", description = "根据bizNo获取质检单据-样本检测项-明细", method = "GET")
    public Result<List<InspectionBillCheckDetail>> listBySimple(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,
                                                           @Parameter(description="样本编号") @PathVariable(name = "simpleCode", required = true) String simpleCode) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo).addCriterion("simpleCode", Operator.EQ, simpleCode);
        List<InspectionBillCheckDetail> inQualityOrderLines = service.list(InspectionBillCheckDetail.class, c);
        return Result.OK(inQualityOrderLines);
    }

    @GetMapping("listbydetect/{bizNo}/{detectCode}")
    @Operation(summary = "根据bizNo,detectCode获取对应质检单据-样本检测项-明细", description = "根据bizNo,detectCode获取质检单据-样本检测项-明细", method = "GET")
    public Result<List<InspectionBillCheckDetail>> listByDetect(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,
                                                           @Parameter(description="检测项编号") @PathVariable(name = "detectCode", required = true) String detectCode) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo).addCriterion("detectCode", Operator.EQ, detectCode);
        List<InspectionBillCheckDetail> inQualityOrderLines = service.list(InspectionBillCheckDetail.class, c);
        return Result.OK(inQualityOrderLines);
    }

    @RequestMapping("check/{bizNo}")
    @Operation(summary = "检测明细", description = "检测明细")
    public Result<Object> excute(@PathVariable(name = "bizNo", required = true) String bizNo,
                                 @RequestBody List<InspectionBillCheckDetail> details) {
        InspectionBill bill = inspectionBillService.getById(InspectionBill.class, bizNo);
        if (bill.getCheckStatus().equals(InspectionEnums.CheckStatus.FINISH.getCode())){
            return Result.error("单据已检验完成，不能提交检测！");
        }
        return service.checkDetails(bill,details);
    }

}
